- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path898. Bitwise ORs of Subarrays.go
53 lines (50 loc) · 925 Bytes
/
898. Bitwise ORs of Subarrays.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package leetcode
// 解法一 array 优化版
funcsubarrayBitwiseORs(A []int) int {
res, cur, isInMap:= []int{}, []int{}, make(map[int]bool)
cur=append(cur, 0)
for_, v:=rangeA {
varcur2 []int
for_, vv:=rangecur {
tmp:=v|vv
if!inSlice(cur2, tmp) {
cur2=append(cur2, tmp)
}
}
if!inSlice(cur2, v) {
cur2=append(cur2, v)
}
cur=cur2
for_, vv:=rangecur {
if_, ok:=isInMap[vv]; !ok {
isInMap[vv] =true
res=append(res, vv)
}
}
}
returnlen(res)
}
funcinSlice(A []int, Tint) bool {
for_, v:=rangeA {
ifv==T {
returntrue
}
}
returnfalse
}
// 解法二 map 版
funcsubarrayBitwiseORs1(A []int) int {
res, t:=map[int]bool{}, map[int]bool{}
for_, num:=rangeA {
r:=map[int]bool{}
r[num] =true
forn:=ranget {
r[(num|n)] =true
}
t=r
forn:=ranget {
res[n] =true
}
}
returnlen(res)
}